package eerr.llrr.oorr.pte9983.wifi4392.jump98001

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
//import com.b.w.utils.CheckUtils
import eerr.llrr.oorr.pte9983.b.pl190.host668.AddUtils
import eerr.llrr.oorr.pte9983.wifi4392.jump98001.RouteRegister.getClass
import yyee.llee.mmee.pl190.host668.RouteJumpInterface
import yyee.llee.mmee.pl190.host668.StrongContext
import com.manager.core.ad.tip.ActivityFun
import com.manager.core.intercept.AdInterceperManager
import com.manager.core.pl190.host668.AdFlow
import com.manager.core.pl190.host668.AdLoadMana
import com.manager.core.pl190.host668.AdRealInterceptorChain
import com.manager.core.pl190.host668.AdReponseCallback
import com.manager.core.pl190.host668.JWAdBase
import com.manager.core.pl190.host668.handle.HandlerPostAdInterface
import be.CQ

object RouteJump : RouteJumpInterface {


    var mOTJAction: Boolean = false
    private const val TAG = eerr.llrr.oorr.activitys.Constant3289.TAG + "_RouteJump"
    private var lastTime: Long = 0
    private var mLastBaseAd: JWAdBase? = null
    override fun safeJump(key: String) {
        safeJump(StrongContext.getContext(), key)
    }


    override fun safeJump(context: Context, key: String) {
        AdInterceperManager.getInstance().removeAllHanlde()
        AdRealInterceptorChain.getInstance().cancel()

        if (!TextUtils.isEmpty(key)) {
            if (!eerr.llrr.oorr.pte9983.wifi4392.jump98001.RouteManager.getInstance()
                    .isCanShow(key)
            ) {
                return
            }
            val aClass = getClass(key)
            if (aClass == null) {
                return
            }
            // 验证 ---------------------- start
            if (yyee.llee.mmee.pl190.host668.RouteKey.LOCK_NEWS != key) {
                AdLoadMana.getInstance().preLoading(
                    StrongContext.getContext(
                        StrongContext.getContext()
                    ),
                    key,
                    object : AdReponseCallback {
                        override fun onResponse(adFlow: AdFlow?) {
                            StrongContext.clearAdActivity()
                            val intent = Intent(context, aClass)
                            intent.putExtra(HandlerPostAdInterface.HANDLE_ADINDEX, 0)
                            realJumpByIntent(context, key, intent)
                        }
                    })
                return@safeJump
            } else {
            }
            /**
             * end
             */
            StrongContext.clearAdActivity()
            val intent = Intent(context, aClass)
            realJumpByIntent(context, key, intent)
        }
    }

    override fun safeJumpByKeyAndClass(key: String, clazz: Class<AppCompatActivity>) {

        val intent = Intent(StrongContext.getContext(), clazz)
        realJumpByIntent(StrongContext.getContext(), key, intent)
    }

    override fun safeJumpByIntent(key: String, intent: Intent) {
        realJumpByIntent(StrongContext.getContext(), key, intent)

    }

    override fun safeJumpDispatcherIntent(key: String, intent: Intent) {

        intent.setClass(StrongContext.getContext(), CQ::class.java)
        safeJumpByIntent(key, intent);
    }

    /**
     * 跳转intent
     * @param context
     * @param key
     * @param intent
     */
    override fun realJumpByIntent(context: Context, key: String, intent: Intent) {
        intent.putExtra(yyee.llee.mmee.pl190.host668.RouteKey.ROUTE_FROM, key)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
        yyee.llee.mmee.pl190.host668.UTJEventUtils.outRequest(context, key)
        val uniId = yyee.llee.mmee.pl190.host668.otj.JEventTj.randomUniId()
        intent.putExtra(yyee.llee.mmee.pl190.host668.otj.JEventTj.OUTPAGE_KEY, uniId)
        if (!mOTJAction) {
            mOTJAction = true
            yyee.llee.mmee.pl190.host668.otj.OTJ.onReq(key, uniId)
        }
        onJump(key, context, intent)

    }


    override fun onJump(key: String, context: Context, intent: Intent) {
        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "onJump: > 1s to jump $key")
        eerr.llrr.oorr.activitys.utim986.BLog.d("TTMediationSDK", "onJump: > 1s to jump $key")

        // RouteJump已经在插件中了, 直接调用插件中的方法
        // ChannelUtils.startSW(context.applicationContext,intent)

//        var foreground = true
//        val appContext = context.applicationContext
//        if (appContext is BaseApplication) {
//            foreground = appContext.isForeground()
//        }
//        BLog.e("Notification_Plu", "onJump: key:$key, isVivo:${RomUtil.isVivo()}, sdk:${Build.VERSION.SDK_INT}, foreground:$foreground")
//        // vivo手机 且 Android 10、11、12弹通知栏
//        if (RomUtil.isVivo() && Build.VERSION.SDK_INT >= 28 && Build.VERSION.SDK_INT <= 32 && !foreground) {
//            MyNotificationManager.show(context, key)
//        } else {
//            MyNotificationManager.cancel(context)
//            LogUtils.logEE(context, intent)

//        CheckUtils.safeStart()
        if (!eerr.llrr.oorr.pte9983.b.utils.StarterUtil.safeStart(context, intent)) {
            ActivityFun.startActivityDelay(context, intent)
        }
    }

    /**
     * 场景 ：  体外进入体内走开屏再走体内
     *
     * openApi 注册中心
     * inPage.put(PAGE_JIASU, NetAccelerateActivity.class);
     * inPage.put(PAGE_SAFE, NetDetectionActivity.class);
     * inPage.put(PAGE_TEST_SPEED, NetSpeedActivity.class);
     * inPage.put(PAGE_CLEAN, RubishActivity.class);
     * inPage.put(PAGE_COOL, CoolActivity.class);
     * @param path
     * @param activity
     */
    override fun registerOpenApi(path: String?, activity: Class<out AppCompatActivity?>?) {
        if (TextUtils.isEmpty(path) || activity == null) {
            return
        }
        inPage[path] = activity
    }

    /**
     * 跳转 体内功能页
     * @param activity
     * @param key
     */
    override fun jumpWelcomInPageByKey(activity: Activity?, key: String) {
        if (!TextUtils.isEmpty(key) && activity != null) {
            val aClass = inPage[key]
            if (aClass == null) {
                return
            }
            val intent = Intent(activity, yyee.llee.mmee.pl190.host668.BFactory.getWelcomActivity())
            intent.putExtra("OpenApi", "outpage:$key")
            activity.startActivity(intent)
        }
    }


    // --------------------------- openApi  ---------------------------
    val inPage: MutableMap<String?, Class<*>> = HashMap()
}